package com.zy.service;

import jakarta.mail.internet.MimeMessage;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

/**
 * @program: shopping153_cloud
 * @description: 邮件发送类
 * @author: zy
 * @create: 2025-08-31 10:44
 */
@Service
@Log
public class MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    public void send(  String  to, String from,String subject, String content ){
        MimeMessage mm=javaMailSender.createMimeMessage();   //可以包括附件
        try {
            //                                                      是否有附件,    编码格式
            MimeMessageHelper message = new MimeMessageHelper(mm, true, "utf-8");
            message.setFrom(from);  //谁发
            message.setTo(to);    //发给谁
            message.setSubject(subject);   //邮件的标题
            message.setText(content, true);    //   一定  true ,代表发过去的是html.

            javaMailSender.send(mm);
        }catch(Exception ex){
            ex.printStackTrace();
            log.severe("发送邮件失败"+ex.getMessage());
        }


    }

}
